//
//  VRMyAdressViewController.m
//  VRFirefighting
//
//  Created by Veer on 2018/8/19.
//  Copyright © 2018年 Veer. All rights reserved.
//

#import "VRMyAdressViewController.h"

#import "VRMyAdressCell.h"
#import "VRMyAdressModel.h"

@interface VRMyAdressViewController () <UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) UIView *contentView;

@property (nonatomic, strong) NSMutableArray *addressArray;

@property (nonatomic, strong) NSArray<VRMyAdressModel *> *modelArray;

@end

@implementation VRMyAdressViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = ScrollViewBgColor;
    self.title = @"我的地址设置";
    self.addressArray = [NSMutableArray array];
    
    [self setupSubView];
    [self getHomeAddress];
}

- (void)setupSubView
{
    UIView *contentView = [[UIView alloc] init];
    contentView.frame = CGRectMake(0, 0, kScreenWidth, 60);
    self.tableView.tableFooterView = contentView;
    
    UILabel *titleLbl = [[UILabel alloc] init];
    titleLbl.text = @"客服热线：400-8533-888";
    titleLbl.textColor = RGB(80, 80, 80);
    titleLbl.font = VRFont(16);
    [contentView addSubview:titleLbl];
    [titleLbl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(contentView).offset(0);
        make.left.mas_equalTo(20);
    }];
    
}

- (void)refreshPage
{
    
    UILabel *lastLbl = nil;
    for (int i = 0; i < self.addressArray.count; i++) {
        
        NSString *addressStr = self.addressArray[i];
        
        if (i == 0) {
            UILabel *addTitleLbl = [[UILabel alloc] init];
            addTitleLbl.text = @"我的地址：";
            addTitleLbl.textColor = RGB(80, 80, 80);
            addTitleLbl.font = VRFont(16);
            [self.contentView addSubview:addTitleLbl];
            [addTitleLbl mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.mas_equalTo(0);
                make.left.mas_equalTo(30);
                make.height.mas_equalTo(30);
            }];
        }
        
        UILabel *titleLbl = [[UILabel alloc] init];
        titleLbl.text = addressStr;
        titleLbl.textColor = RGB(80, 80, 80);
        titleLbl.font = VRFont(16);
        [self.contentView addSubview:titleLbl];
        [titleLbl mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(i * 30);
            make.left.mas_equalTo(104);
            make.height.mas_equalTo(30);
        }];
        
        lastLbl = titleLbl;
    }
    
    [self.contentView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(30 * self.addressArray.count);
    }];
}

#pragma mark - networking
- (void)getHomeAddress
{
    
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    param[@"UserID"] = [VRUser shareUser].token;
    
    weakify(self)
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    //    hud.bezelView.backgroundColor = RGBA(0, 0, 0, 0.9);
    //    hud.bezelView.style = MBProgressHUDBackgroundStyleBlur;
    hud.mode = MBProgressHUDModeIndeterminate;
    [self.addressArray removeAllObjects];
    [JKNetworking postWithSign:GetMyAddress parameters:param success:^(NSDictionary *responseObject) {
        
        strongify(self)
        NSLog(@"responseObject:%@", responseObject);
        NSInteger code = [responseObject[@"state"] integerValue];
        NSString *msg = responseObject[@"message"];
        
        if (code == 0) {
            
            self.modelArray = [VRMyAdressModel mj_objectArrayWithKeyValuesArray:responseObject[@"data"]];
            dispatch_async(dispatch_get_main_queue(), ^{
                [self.tableView reloadData];
                [hud hideAnimated:YES];
            });
        }else if(msg.length > 0){
            dispatch_async(dispatch_get_main_queue(), ^{
                [SVProgressHUD showErrorWithStatus:msg];
                [hud hideAnimated:YES];
            });
        }
    } error:^(NSError * _Nonnull error) {
        
        dispatch_async(dispatch_get_main_queue(), ^{
            [SVProgressHUD showErrorWithStatus:@"未知错误"];
            [hud hideAnimated:YES];
        });
    }];
}

#pragma mark - delegate
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.01;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

    return self.modelArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    return 70;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    VRMyAdressCell *cell = [VRMyAdressCell cellWith:tableView];
    cell.model = self.modelArray[indexPath.row];
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
}


#pragma mark - lazy
- (UITableView *)tableView
{
    if (_tableView == nil) {
        
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight) style:UITableViewStylePlain];
        _tableView.backgroundColor = ScrollViewBgColor;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.tableHeaderView = [[UIView alloc] init];
        if (@available(iOS 11.0, *)) {
//            _tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        }
        
        UIView *view1 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 0)];
        _tableView.tableFooterView = view1;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.estimatedRowHeight = 0;
        _tableView.estimatedSectionHeaderHeight = 0;
        _tableView.estimatedSectionFooterHeight = 0;
        _tableView.sectionFooterHeight = 0.01;
        [self.view addSubview:self.tableView];
        //        [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        //            make.left.right.bottom.mas_equalTo(self.view);
        //            make.top.mas_equalTo(0);
        //        }];
    }
    return _tableView;
}


@end
